package com.mlnx.ecg.ui.view;

import com.mlnx.ecg.ui.model.Point;
import com.mlnx.ecg.ui.model.TimePoint;
import com.mlnx.ecg.ui.model.WavePos;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class TimeWaveView extends WaveView {

    private List<TimePoint> choseTimePoints = new ArrayList<>();

    public TimeWaveView(WavePos wavePos, int flag, String describe, int showFre, float mmPermV, float mmPerS) {
        super(wavePos, flag, describe, showFre, mmPermV, mmPerS);
    }

    @Override
    public synchronized void init() {
        super.init();

        points = new TimePoint[np];
        for (int i = 0; i < points.length; i++) {
            points[i] = new TimePoint(i * dataXInterval + left + offset, 0, null);
        }
    }

    public synchronized void updatePoint(float v, long time, String no) {
        if (!animing) {
            TimePoint timePoint = (TimePoint) points[cur_point];

            timePoint.y = v == -1000f ? INVALID_DATA : (v * svCell * cellNumbPremV);
            timePoint.time = time;
            timePoint.no = no;

            if (cur_point + 1 >= np)
                return;

            cur_point = (cur_point + 1) % np;
        }
    }

    public List<TimePoint> setFlag(int x, int y) {
        Float juli = null;
        TimePoint timePoint = null;
        for (int i = 0; i < points.length; i++) {
            if (juli == null) {
                juli = Math.abs(x - points[i].x);
            } else {
                float temp = Math.abs(x - points[i].x);
                if (juli > temp) {
                    juli = temp;
                } else {
                    timePoint = ((TimePoint) points[i - 1]);
                    break;
                }
            }
        }

        if (timePoint != null) {
            if (choseTimePoints.size() >= 2) {
                choseTimePoints.clear();
            }
            choseTimePoints.add(timePoint);
        }

        return choseTimePoints;
    }

    public void clearChoseTimePoints() {
        choseTimePoints.clear();
    }

    @Override
    public synchronized void doDraw(Graphics2D g, boolean drawPoint, Color bgColor, Color gridColor, Color textColor,
                                    Color ecgColor) {
        super.doDraw(g, drawPoint, bgColor, gridColor, textColor, ecgColor);

        List<Point> temp = new ArrayList<>();
        for (Point point : points) {
            TimePoint timePoint = (TimePoint) point;
            for (TimePoint timePoint2 : choseTimePoints) {
                if (timePoint2.no != null && timePoint2.no.equals(timePoint.no)) {
                    g.setColor(new Color(76, 169, 233));
                    g.drawLine((int) timePoint.x, (int) lTop.y, (int) timePoint.x, (int) lBot.y);
                    temp.add(point);
                }
            }
        }

        if (temp.size() >= 2) {
            g.setColor(new Color(76, 169, 233, 60));
            g.fillRect((int) temp.get(0).x + 1, (int) lTop.y, (int) (temp.get(1).x - temp.get(0).x - 1),
                    (int) (lBot.y - lTop.y));
        }
    }

}
